// pages/my/my.js
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginOK: false,
    name: '游客' + Math.floor(Math.random() * 100000),
    headImg: 'https://i0.hdslb.com/bfs/face/member/noface.jpg@160w_160h_1c_1s.webp',
    userId: '',
    user: {}
  },

  headimage() {
    if (!this.data.user?.usercode) {
      wx.navigateTo({
        url: '/pages/login/login',
      });
      return;
    }
    wx.chooseMedia({
      count: 1,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success: (res) => {
        let path = res.tempFiles[0].tempFilePath
        let spitArr = path.split('/')
        let cloudPath = 'user/' + spitArr[spitArr.length - 1]
        // console.log(cloudPath);
        wx.cloud.uploadFile({
          cloudPath: cloudPath,
          filePath: path,
          success: res => {
            // console.log(res.fileID, '上传成功2');
            this.setData({
              headImg: res.fileID
            });
            let user = {
              ...this.data.user
            };
            user.img = res.fileID;
            this.setData({
              user
            });
            wx.cloud.callFunction({
              name: "changeUser",
              data: {
                user
              }
            });
          }
        });
      }
    })
  },

  // 去到登录页面
  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  // 去修改信息
  goUserInfo() {
    if (!this.data.user?.usercode) {
      wx.navigateTo({
        url: '/pages/login/login',
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/userInfo/userInfo',
    })
  },

  // 去到阅读页
  goReadHistory() {
    wx.navigateTo({
      url: '/pages/readhistory/readhistory',
    })
  },
  // 退出登录
  quitLogin() {
    // console.log('退出登录');
    wx.setStorageSync('user', null)
    let user = wx.getStorageSync('user')
    console.log(user, 'user');
    if (user && user.name) {
      this.setData({
        loginOK: true,
        name: user.name
      })
    } else {
      this.setData({
        loginOK: false,
        name: '用户尚未登录',
        headImg: 'https://i0.hdslb.com/bfs/face/member/noface.jpg@160w_160h_1c_1s.webp'
      })
    }
  },

  // 敬请期待
  goToWork() {
    wx.showToast({
      icon: "none",
      title: '敬请期待'
    })
  },

  // 设置夜间
  setDark() {
    wx.showToast({
      icon: 'none',
      title: '开启夜间',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let user = wx.getStorageSync('user')
    let usercode = user.usercode

    if (!usercode) {
      return
    }
    db.collection('user').where({
      usercode: usercode
    }).get().then((res) => {
      let cxuser = res.data[0]
      if (cxuser && cxuser.usercode) {
        this.setData({
          loginOK: true,
          name: cxuser.name,
          user: cxuser,
          headImg: cxuser.img
        })
      } else {
        this.setData({
          loginOK: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})